<template>
	<view class="comments-box">
		<view class="comments-header">
			<view class="comments-header_logo">
				<image :src="comments.author.avatar" mode="aspectFill"></image>
			</view>
			<view class="comments-header_info">
				
				<view v-if="!comments.is_reply" class="title">{{comments.author.author_name}}</view>
				<view v-else class="title">{{comments.author.author_name}}<text class="replay_text">回复</text>{{comments.to}}</view>
				
				<view>{{comments.create_time | formaTime}}</view>
			</view>
		</view>
		<view class="comments-content">
			<view>{{comments.comment_content}}</view>
			<view class="comments-info">
				<view class="comments-button" @click="commentsReply({comments:comments, is_reply:reply})">回复</view>
			</view>
			<view class="comments-repaly" v-for="(item, index) in comments.replys" :key="index"><!-- :key="comments.comment_id" -->
				<comments-box :reply="true" :comments="item" @reply="commentsReply"></comments-box>
			</view>
		</view>
	</view>
</template>

<script>
	import commentsBox from "@/components/comments-box/comments-box.vue"//组件递归（自己调用自己，避免无限调用，给出条件判断）
	import {parseTime} from "@/utils/index.js"
	
	export default {
	    name:"comments-box",
		components:{
			commentsBox
		},
		props:{
			comments:{
				type:Object,
				default(){
					return {}
				}
			},
			reply:{
				type:Boolean,
				default:false
			}
		},
		filters:{
			formaTime(time){
				return parseTime(time)
			}
		},
		data() {
			return {
				
			};
		},
		onLoad(){
			console.log(this.comments)
		},
		methods:{
			commentsReply(comment){
				//为了区分主回复，还是子回复
				if(comment.is_reply){
					comment.comments.reply_id = comment.comments.comment_id
					comment.comments.comment_id = this.comments.comment_id //自调用的comment_id
				}
				 console.log(comment)
				this.$emit("reply" ,comment)
				// console.log("回复")
			},
		}
	}
</script>

<style  lang="scss">
.comments-box{
	margin:15px;
	
	.comments-header{
		display: flex;
		
		.comments-header_logo{
			flex-shrink: 0;
			width:30px;
			height:30px;
			border-radius: 5px;
			overflow: hidden;
			
			image{
				width:100%;
				height:100%;
			}
		}
		.comments-header_info{
			display: flex;
			flex-direction: column;
			padding-left: 15px;
			font-size: 12px;
			color:#999;
			line-height: 1;
			
			.title{
				margin-bottom:10px;
				font-size: 14px;
				color:#333;
				
				.replay_text{
					margin:0 10px;
					font-weight: bold;
					color:#000;
				}
			}
		}
	}
	.comments-content{
		margin-top: 10px;
		font-size: 14px;
		color:#000;
		
		.comments-info{
			margin-top: 15px;
			display: flex;
			
			.comments-button{
				padding:2px 10px;
				font-size:12px;
				color:#999;
				border-radius: 20px;
				border:1px #ccc solid;
			}
		}
		.comments-repaly{
			display: flex;
			margin: 15px 0;
			padding:0 10px;
			border:1px #eee solid;
		}
	}
}
</style>
